#encoding: utf-8

import sys
reload(sys) 
sys.setdefaultencoding('utf-8')

# import tkinter

# import tkinter.messagebox

# def main():

# 	flag = True

# 	# 修改标签上的文字
# 	def change_label_text():
# 		nonlocal flag
# 		flag = not flag
# 		color, msg = (('red', 'hello, world!') if flag else ('blue', 'goodbye, world') )
# 		label.config(text=msg, fg = color)	

# 	# 确认退出
# 	def confirm_to_quit():
# 		if tkinter.messagebox.askokcancel('提醒', '退出?'):
# 			top.quit()
# 	# 创建顶层窗口
# 	top = tkinter.Tk()
# 	# 设置窗口大小
# 	top.geometry('240x160')
# 	# 设置窗口标题
# 	top.title('小游戏')
# 	# 创建标签对象并添加到顶层窗口
# 	label = tkinter.Label(top, text = 'hello, world', font = 'Arial -32', fg = 'red')
# 	label.pack(expand = 1)
# 	# 创建一个装按钮的容器
# 	panel = tkinter.Frame(top)
# 	 # 创建按钮对象 指定添加到哪个容器中 通过command参数绑定事件回调函数
# 	button1 = tkinter.Button(panel, text='修改', command = change_label_text)
# 	button1.pack(side ='left')
# 	button2 = tkinter.Button(panel, text= '退出', command = confirm_to_quit)
# 	button2.pack(side = 'right')
# 	panel.pack(side='bottom')
# 	# 开启主事件循环
# 	tkinter.mainloop()

# if __name__ == '__main__':
# 	main()

from enum import Enum, unique
from math import sqrt
from random import randint

import pygame


@unique
class Color(Enum):
	""" 颜色 """
	RED = (255, 0 , 0)
	GREEN = (0, 255, 0)
	BLUE = (0, 0, 255)
	BLACK = (0, 0, 0)
	WHITE = (255, 255, 255)
	GRAY = (242, 242, 242)

	@staticmethod
	def random_color():
		r = randint(0, 255)
		g = randint(0, 255)
		b = randint(0, 255)
		return (r, g, b)


class Ball(object):
	"""docstring for Ball"""
	def __init__(self, x, y, radius, sx, sy, color = Color.RED):
		super(Ball, self).__init__()
		self.x = x
		self.y = y 
		self.radius = radius
		self.sx = sx
		self.sy = sy
		self.color = color
		self.alive = True
		
	def move(self, screen):
		self.x += self.sx
		self.y += self.sy
		if self.x - self.radius <= 0 or self.x + self.radius >= screen.get_width():
			self.sx = -self.sx
		if self.y - self.radius <= 0 or self.y + self.radius >= screen.get_height():
			self.sy = -self.sy

	def eat(self, other):
		if self.alive and other.alive and self != other:
			dx, dy = self.x - other.x, self.y - other.y
			distance = sqrt(dx**2 + dy**2)
			if distance < self.radius + other.radius and self.radius > other.radius:
				other.alive = False
				self.radius = self.radius + int(other.radius *0.146)


	def draw(self, screen):
		pygame.draw.circle(screen,self.color,(self.x, self.y), self.radius, 0)
		pass


def main():
	balls = []
	# 初始化导入的pygame中的模块
	pygame.init()
	# 初始化用于显示的窗口并设置窗口尺寸
	screen = pygame.display.set_mode((800, 600))
	 # 设置当前窗口的标题
	pygame.display.set_caption('大球吃小球')
	 # 设置窗口的背景色(颜色是由红绿蓝三原色构成的元组)
	# screen.fill((255, 255, 255))
	# 绘制一个圆(参数分别是: 屏幕, 颜色, 圆心位置, 半径, 0表示填充圆)
	# pygame.draw.circle(screen, (255, 255, 0), (200, 100), 50 , 0)
	# 通过指定的文件名加载图像
	# ball_image = pygame.image.load('./res/ball.png')
	# 在窗口上渲染图像
	# screen.blit(ball_image, (50, 50))
	# 刷新当前窗口(渲染窗口将绘制的图像呈现出来
	# pygame.display.flip()


	running = True

	while running:
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				running = False

			if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
				x, y = event.pos
				radius = randint(10, 100)
				sx, sy = randint(-10, 10), randint(-10, 10)
				color = Color.random_color()
				ball = Ball(x, y, radius, sx, sy, color)
				balls.append(ball)
		
		screen.fill((255, 255, 255))

		for ball in balls:
			if ball.alive:
				ball.draw(screen)
			else:
				balls.remove(ball)
		pygame.display.flip()
		pygame.time.delay(50)

		for ball in balls:
			ball.move(screen)
			for other in balls:
				ball.eat(other)
				pass
			pass

if __name__ == '__main__':
	main()
























